/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.saber.certificacao.beansjsf.tutoriais.lazytable;

import br.com.saber.certificacao.ejbs.stateless.interfaces.TutorialBeanLocal;
import br.com.saber.certificacao.modelos.Tutorial;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.primefaces.model.LazyDataModel;
import org.primefaces.model.SortOrder;

/**
 *
 * @author Guilherme
 */
public class LazyTutorialModel extends LazyDataModel<Tutorial> {

    private TutorialBeanLocal bean;

    public LazyTutorialModel(TutorialBeanLocal bean) {
        this.bean = bean;
    }

    @Override
    public List<Tutorial> load(int posInicial, int maxResult, String string, SortOrder so, Map<String, String> map) {
        List<Tutorial> toReturn = new ArrayList<Tutorial>();
        Integer linhas = 0;
        String solicitado = map.get("indice");
        if(solicitado ==null){
            solicitado = "";
        }
        String[] indices = solicitado.split(" ");
        for (String indice : indices) {
            toReturn.addAll(bean.buscarTutorialPorIndicePaginado(posInicial, maxResult, indice));
            linhas += bean.buscarQtdadeTutorialPorIndicePaginado(indice);
        }
        setRowCount(linhas);
        return toReturn;
    }
}
